package com.cvit.controller;

import java.beans.IntrospectionException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvit.model.Msg;
import com.cvit.model.vMajor;
import com.cvit.service.vMajorService;

@Controller
public class vMajorController {

	@Autowired
	vMajorService vmajorService;
	
	
	/**
	 * 查询所有
	 * */
	@RequestMapping("/getvMajorAll")
	@ResponseBody
	public Msg getvMajorAll(){
		List<vMajor> vmajor = vmajorService.getvMajorAll();
		return Msg.success().add("majors", vmajor);
	}
	
	/**
	 * 导出Excel表  专业人数表
	 * */
	@RequestMapping("/vmajorExport")  
	public @ResponseBody void export(HttpServletRequest request, HttpServletResponse response) throws Exception {  
		String vmajorData = "专业人数表";
	    if(vmajorData!=""){
			response.reset(); //清除buffer缓存  
	        Map<String,Object> map=new HashMap<String,Object>();  
	        // 指定下载的文件名  
	        response.setHeader("Content-Disposition", "attachment;filename="  
			        + new String(vmajorData.getBytes(),"iso-8859-1") + ".xlsx");
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);   
	        XSSFWorkbook workbook=null;  
	        //导出Excel对象  
	        workbook = vmajorService.exportExcelInfo(vmajorData);  
	        OutputStream output;  
	        try {
	            output = response.getOutputStream();  
	            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);  
	            bufferedOutPut.flush();  
	            workbook.write(bufferedOutPut);  
	            bufferedOutPut.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
}
